import lombok.SneakyThrows;

import java.util.concurrent.Semaphore;

/**
 * 线程间循环交替执行
 */

public class Test {


    private static Semaphore a = new Semaphore(1);
    private static Semaphore b = new Semaphore(1);
    private static Semaphore c = new Semaphore(1);



    public static void main(String[] args) {


        try {
            b.acquire();
            c.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                while (true){
                    a.acquire();
                    System.out.println("A");
                    b.release();

                }
            }
        }).start();

        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                while (true){
                    b.acquire();
                    System.out.println("B");
                    c.release();

                }
            }
        }).start();


        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                while (true){
                    c.acquire();
                    System.out.println("C");
                    a.release();

                }
            }
        }).start();






    }

}
